<form class="layui-form seller-form"  action="" >
    <div class="layui-form-item">

        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">规则名称：</label>
            <div class="layui-input-inline">
                <input type="text" name="name" lay-verify="title" placeholder="请输入规则名称" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">失效时间：</label>
            <div class="layui-input-inline">
                <input type="text" id="expires_in" name="expires_in" lay-verify="title" placeholder="开始时间 到 结束时间" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-inline">
            <button class="layui-btn layui-btn-sm" lay-submit lay-filter="search"><i class="iconfont icon-chaxun"></i>筛选</button>

            <button type="button" class="layui-btn layui-btn-sm add"><i class="layui-icon">&#xe608;</i> 添加</button>
        </div>

    </div>
</form>

<div class="table-body">
	<table id="recharge" lay-filter="recharge"></table>
</div>

<script>
    layui.use(['table','form','layer','laydate'],function(){
        var layer = layui.layer, table = layui.table,form = layui.form,date = layui.laydate;
        // 定义变量保存当前页面和当前页面数据条数
        var currPage; 
        var dataNum;

        //执行渲染
        table.render({
            elem: '#recharge', //指定原始表格元素选择器（推荐id选择器）
            height: 'full-115',
            cellMinWidth: '80',
            page: 'true',
            limit:'20',
            id:'recharge',
            url: "{:url('recharge/index')}",
            cols: [[ //标题栏
                {type:'numbers'},
                {field: 'id', title: '规则ID', align: 'center', width: 200, hide: true },
                {field: 'name', title: '规则名称',align:'center',width:120},
                {field: 'money', title: '充值金额',align:'center',width:120},
                {field: 'gift', title: '赠送金额',align:'center',width:120},
                {field: 'desc', title: '规则说明',align:'center',width:200},
                {field: 'expires_in', sort: true, title: '失效时间' ,align:'center',width:160}, 
                {field: 'sort', sort: true, title: '排序',align:'center',width:80},
                {field: 'is_pub', title: '状态', sort: true, align: 'center', width: 150, templet: '#is_pub', unresize: true },
                {width:150, title:'操作',align:'center', toolbar:'#rechargeBar',width:160}
            ]], //设置表头
            done: function(res, curr, count){
                // 给变量赋值 
                dataNum = res.data.length;
                currPage = curr;
            }
        });

        date.render({
            elem:'#expires_in',
            range: '到'
        });

        form.on('submit(search)', function(data){
            layui.table.reload('recharge', {
                where: data.field
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //添加
        $(document).on('click','.add',function(){
            var topLayui = parent === self ? parent.window.layui : top.parent.window.layui;
            topLayui.index.openTabsPage('{:url("recharge/add")}', '添加充值规则'); 
            return false;
        });

        //监听 操作状态
        form.on('switch(is_pub)', function (obj) {
            JsPost("{:url('recharge/changeStatus')}", { id: this.value, is_pub: obj.elem.checked }, function (res) {
                layer.msg(res.msg);
            });
        });

        //监听工具条
        table.on('tool(recharge)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if(layEvent === 'del'){ //删除
                layer.confirm('真的要删除么',{icon: 3}, function(index){
                    JsGet("{:url('recharge/del')}?id=" + data.id, function(res){
                        //删除逻辑中判断只有一条数据时应该刷新前一页信息
                        layer.msg(res.msg);
                        if(res.status){
                            if(dataNum == 1){
                                currPage = currPage - 1;
                            }

                            table.reload('recharge', {
                                page: {
                                    curr: currPage
                                }
                            });
                            return false;
                        }
                    })
                });
            } else if(layEvent === 'edit'){ //编辑 
                var editUrl = "{:url('recharge/edit')}?id="+data.id;
                var topLayui = parent === self ? parent.window.layui : top.parent.window.layui;
                topLayui.index.openTabsPage(editUrl, '编辑充值规则'); 
                return false;
            }
        });

    })
</script>

<script type="text/html" id="is_pub">
    <input type="checkbox" name="is_pub" value="{{d.id}}" lay-skin="switch" lay-text="启用|停用" lay-filter="is_pub" {{ d.is_pub == 1 ? 'checked' : '' }}>
</script>

<script type="text/html" id="rechargeBar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
